package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.service.UserCountService;

@RestController
@RequestMapping("v1/usercount")
public class UserCountController {
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;
	
	@Autowired
	private UserCountService userCountService;

	
	@RequestMapping(value = "/pie", method = { RequestMethod.GET })
	public Map<String, Object> pie(Principal principal,Long networkId,Byte type) {
		if(networkId == null || networkId < 0 || type == null || type < 0){
			RestResult result = new RestResult();
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		RestResult result = userCountService.getPieByType(networkId,type);
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/line", method = { RequestMethod.GET })
	public Map<String, Object> line(Principal principal,Long networkId,Integer range) {
		if(networkId == null || networkId <= 0 || range == null || range < 1 || range > 3){
			RestResult result = new RestResult();
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		RestResult result = userCountService.getLine(networkId,range);
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/refresh", method = { RequestMethod.GET })
	public Map<String, Object> refresh(Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		userCountService.refresh(user.getGroupId());
		return result.toJsonObject();
	}
	
}
